package week12;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jms.core.JmsTemplate;
import week12.entity.Order;

import java.util.List;
import java.util.UUID;

//
@EnableAspectJAutoProxy
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication implements ApplicationRunner {
    @Autowired
    JmsTemplate jmsTemplate;
    @Value("${week12.destination}")
    String destination;

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = new SpringApplicationBuilder(DemoApplication.class)
                .web(WebApplicationType.NONE).run(args);
        JmsTemplate jmsTemplate = ctx.getBean(JmsTemplate.class);
        System.out.println(jmsTemplate);
    }

    @Override
    public void run(ApplicationArguments args) {
        //生产者
        new Thread(() -> {
            while (true) {
                int size = 10;
                for (int i = 0; i < size; i++) {
                    Order ord = new Order();
                    ord.setOrderSn(UUID.randomUUID().toString().substring(0, 16));
                    ord.setOrderStatus("0");
                    ord.setUserId(1L);
                    ord.setIsPay(1);
                    jmsTemplate.convertAndSend(destination, JSON.toJSONString(ord));
                }
                System.out.println("生产了" + size + "个订单");
                //orderService.saveBatch(orderList);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
